// import { lk_buyer,lk_common,xc_url1,xc_url2,hhm_url1,hhm_url2,hhm_url3  } from './myConfig.js';
import { lk_buyer, lk_common, lk_expert, lk_seller, lk_manager, lk_consumer } from './myConfig.js';

// 对网络请求重新封装，等待登陆后请求，防止token失败 //不用
function request(method, url, data, header) {
	return new Promise((resolve, reject) => {
		if (getApp().globalData._IsLogin_) {
			// console.warn("直接登陆："+url);
			requestX(method, url, data, header).then(function (data) {
				resolve(data);
			}).catch(function (data) {
				reject(data);
			})
		} else {
			// 设置定时器，等待登陆完成
			let cnt = 0;
			let tid = setInterval(() => {
				if (cnt > 500) { //设置最大等待时间
					clearInterval(tid);
					reject("请求超时，请刷新重试！");
					return;
				}
				// console.warn("等待登陆完成："+(++cnt));
				if (getApp().globalData._IsLogin_) {
					// console.warn("等待登陆成功："+cnt+" ---"+url);
					clearInterval(tid);
					requestX(method, url, data, header).then(function (data) {
						resolve(data);
					}).catch(function (data) {
						reject(data);
					})
				}
			}, 10)
		}
	})
}

function requestX(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'Authorization': uni.getStorageSync('accessToken'),
		'Authorization_jbs': uni.getStorageSync('accessToken')
	}, header)
	return new Promise((resolve, reject) => {
		// uni.hideLoading();
		uni.request({
			url,
			method,
			data,
			header: headers,
			success: ({
				data: res
			}) => {
				//  uni.hideLoading() 
				if (res && res.code.code === 200) {
					try {
						const _data_ = JSON.parse(res.data || '{}');
						resolve(_data_);
					} catch (error) {
						resolve(res.data);
					}
				} else {
					uni.showToast({
						title: res.message || '服务器响应超时,请稍后再试！',
						icon: 'none'
					});
					reject(res.message);
				}
			},
			fail: function (res) {
				console.log(res, 9999)
				uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res.data);
			}
		});
	})
}

function requesthttpLogin(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
	}, header)
	return new Promise((resolve, reject) => {
		uni.hideLoading();
		uni.request({
			url,
			method,
			data,
			header: headers,
			success: ({
				data: res
			}) => {
				uni.hideLoading();
				resolve(res);
			},
			fail: function (res) {
				uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res);
			}
		});
	})
}

function requesthttp(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'accessToken': uni.getStorageSync('accessToken'),
		'refreshToken': uni.getStorageSync('accessToken')
		// 'Authorization': uni.getStorageSync('token'),
		// 'Authorization_jbs': uni.getStorageSync('token'),
		// 'accessToken':'eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyQ29udGV4dCI6IntcInVzZXJuYW1lXCI6XCIxODMyODAzNTAxM1wiLFwibmlja05hbWVcIjpcIuadjuaYjlwiLFwiZmFjZVwiOlwiaHR0cDovLzE0MC4yMTAuMjAxLjE1ODo5MDExL28yby83M2ZjNmM3MzQ5ZGU0OTRkYjFmYzBlNjY5ZDg1MmU3ZS5wbmdcIixcImlkXCI6XCIxNTc4MjA5MzQ1MjMyNTMxNDU2XCIsXCJsb25nVGVybVwiOmZhbHNlLFwicm9sZVwiOlwiTUVNQkVSXCIsXCJpc1N1cGVyXCI6ZmFsc2V9Iiwic3ViIjoiMTgzMjgwMzUwMTMiLCJleHAiOjE2NjkzNTMwMzB9.cR5x02w6n-e7lQhuC0s9-3FG28JBATGVvNrr9PtuzE0'
	}, header)
	return new Promise((resolve, reject) => {
		uni.hideLoading();
		uni.request({
			url,
			method,
			data,
			header: headers,
			success: ({
				data: res
			}) => {
				uni.hideLoading();
				resolve(res);
			},
			fail: function (res) {
				uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res);
			}
		});
	})
}

function requestlist(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'Authorization': uni.getStorageSync('accessToken'),
		'Authorization_jbs': uni.getStorageSync('accessToken')
	}, header)
	return new Promise((resolve, reject) => {
		if (getApp().globalData._IsLogin_) {
			// console.warn("直接登陆："+url);
			requestlistX(method, url, data, header).then(function (data) {
				resolve(data);
			}).catch(function (data) {
				reject(data);
			})
		} else {
			// 设置定时器，等待登陆完成
			let cnt = 0;
			let tid = setInterval(() => {
				if (cnt > 500) { //设置最大等待时间
					clearInterval(tid);
					reject("请求超时，请刷新重试！");
					return;
				}
				// console.warn("等待登陆完成："+(++cnt));
				if (getApp().globalData._IsLogin_) {
					// console.warn("等待登陆成功："+cnt+" ---"+url);
					clearInterval(tid);
					requestlistX(method, url, data, header).then(function (data) {
						resolve(data);
					}).catch(function (data) {
						reject(data);
					})
				}
			}, 10)
		}
	})
}

function requestlistX(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'Authorization': uni.getStorageSync('accessToken'),
		'Authorization_jbs': uni.getStorageSync('accessToken')
	}, header)
	return new Promise((resolve, reject) => {
		// uni.hideLoading();
		uni.request({
			url,
			method,
			data,
			header: headers,
			success(res) {
				// uni.hideLoading();
				resolve(res.data);
			},
			fail: function (res) {
				// uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res);
			}
		});
	})
}

function requestRight(method, url, data, header) {
	let headers = Object.assign({
		'content-type': 'application/json',
		'Authorization': uni.getStorageSync('accessToken'),
		'Authorization_jbs': uni.getStorageSync('accessToken'),
		'accessToken': uni.getStorageSync('accessToken'),
	}, header)
	return new Promise((resolve, reject) => {
		// uni.hideLoading();
		uni.request({
			url: url,
			method,
			header: headers,
			success(res) {
				// uni.hideLoading();
				resolve(res.data);
			},
			fail: function (res) {
				// uni.hideLoading();
				uni.showToast({
					title: res.message || '网络响应超时,请稍后再试！',
					icon: 'none'
				});
				reject(res);
			}
		});
	})
}

export const yjApi = {
	/**
	* @param id 
	* @description 工具栏
	* @detail 
	* @author yj
	*/
	getCourseBar: (data) => requestRight('GET', `${lk_expert}/courseCategory/list`, {
		'content-type': 'application/x-www-form-urlencoded'
	}),
	/**
	* @param id 
	* @description 课程列表
	* @detail 
	* @author yj
	*/
	getCourseList: (data) => requesthttp('POST', `${lk_expert}/courseSeries/pagelist`, data, {
		'content-type': 'application/json',
		'accessToken': 'eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyQ29udGV4dCI6IntcInVzZXJuYW1lXCI6XCIxMzAxMTExMTExMVwiLFwibmlja05hbWVcIjpcImphY2tcIixcImZhY2VcIjpcIlwiLFwiaWRcIjpcIjEzNzY0MTc2ODQxNDAzMjY5MTJcIixcImxvbmdUZXJtXCI6ZmFsc2UsXCJyb2xlXCI6XCJNRU1CRVJcIixcImlzU3VwZXJcIjpmYWxzZX0iLCJzdWIiOiIxMzAxMTExMTExMSIsImV4cCI6MTY2OTQ5MDI0M30.dJbunCXO8D8QvvO2mluQ4JuZ9gCJuPemiHDsLoF0Tgk'
	}),
	/**
	* @param id 
	* @description 课程详情介绍
	* @detail 
	* @author yj
	*/
	getCourseIntroduce: (data) => requestRight('GET', `${lk_expert}/expert/course/courseSeries/${data.id}`, {

		'content-type': 'application/json',
	}),

	/**
	* @param id 
	* @description 课程详情目录
	* @detail 
	* @author yj
	*/
	getCourseDirectory: (data) => requestRight('GET', `${lk_expert}/expert/course/course/getCourseByCourseSeriesId/${data.id}`, {
		'content-type': 'application/x-www-form-urlencoded'
	}),

	/**
	* @param { id }
	* @description 课程详情评论
	* @detail 
	* @author yj
	*/
	getCourseComments: (data) => requesthttp('POST', `${lk_manager}/courseSeriesBaseEvaluate/listByPage`, data, {
		'content-type': 'application/json'
	}),

	/**
	* @param id 
	* @description 课程详情评论汇总
	* @detail 
	* @author yj
	*/
	getCourseCommentsAll: (data) => requestRight('GET', `${lk_manager}/courseSeriesBaseEvaluate/getEvaluateCount/${data.id}`, {
		'content-type': 'application/x-www-form-urlencoded'
	}),

	/**
	* @param { id }
	* @description 课程详情推荐
	* @detail 
	* @author yj
	*/
	getCourseRecommended: (data) => requesthttp('POST', `${lk_expert}/expert/course/courseSeries/courseRecommendation`, data, {
		'content-type': 'application/json'
	}),
	/**
	* @param { id }
	* @description 免费加入学习
	* @detail 
	* @author yj
	*/
	joinStudy: (data) => requesthttp('POST', `${lk_manager}/userCourse/userCourseOrder`, data, {
		'content-type': 'application/json'
	}),

	/**
	* @param { id }
	* @description 获取基地列表
	* @detail 
	* @author yj
	*/
	getBase: (data) => requestRight('GET', `${lk_manager}/trainingBase/list`, {
		'content-type': 'application/x-www-form-urlencoded'
	}),

	/**
	* @param { id }
	* @description 基地详情
	* @detail 
	* @author yj
	*/
	getBaseDetail: (data) => requestRight('GET', `${lk_manager}/trainingBase/${data.id}`, {
		'content-type': 'application/x-www-form-urlencoded'
	}),

	/**
	* @param { id }
	* @description 获取场地
	* @detail 
	* @author yj
	*/
	getSite: (data) => requestRight('GET', `${lk_expert}/expert/site/site/list`, {
		'content-type': 'application/x-www-form-urlencoded'
	}),

	/**
	* @param { id }
	* @description 获取场地日期
	* @detail 
	* @author yj
	*/
	getSiteDate: (data) => requestRight('GET', `${lk_expert}/site/isReservable/${data.id}`, {
		'content-type': 'application/x-www-form-urlencoded'
	}),

	/**
	* @param { date,week }
	* @description 专家列表
	* @detail 
	* @author yj
	*/
	getDoctor: (params) => requestRight('GET', `${lk_expert}/siteRoomTime/allTimePeriodsBySiteId/${params.siteId}/${params.week}`, {
		'content-type': 'application/x-www-form-urlencoded'
	}),
	/**
	* @param { expertId }
	* @description 专家点赞
	* @detail 
	* @author yj
	*/
	goodDoctor: (data) => requestRight('GET', `${lk_expert}/expert/expert/expert/liked/${data.expertId}`, {
		'content-type': 'application/x-www-form-urlencoded'
	}),
}